package com.jsrj.controller;

import com.jsrj.common.annotation.Log;
import com.jsrj.common.config.RuoYiConfig;
import com.jsrj.common.core.controller.BaseController;
import com.jsrj.common.core.domain.AjaxResult;
import com.jsrj.common.enums.BusinessType;
import com.jsrj.common.utils.file.FileUploadUtils;
import com.jsrj.domain.Banner;
import com.jsrj.service.IBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2022-09-04
 */
@RestController
@RequestMapping("/system/banner")
public class BannerController extends BaseController
{
    @Autowired
    private IBannerService bannerService;

    /**
     * 新增【添加轮播图】
     */
    @PreAuthorize("@ss.hasPermi('system:banner:add')")
    @Log(title = "【添加轮播图】", businessType = BusinessType.INSERT)
    @PostMapping("/addBanner")
    public AjaxResult add(@RequestParam("file") MultipartFile file)
    {
        Banner banner = new Banner();
        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath();
        // 上传并返回新文件名称
        String fileNewName = null;
        try {
            fileNewName = FileUploadUtils.upload(filePath, file);
            banner.setBannerUrl(filePath+fileNewName.substring(15));
//            banner.setBannerUrl("/upload"+fileNewName.substring(15));
        } catch (IOException e) {
            e.printStackTrace();
        }
        bannerService.insertBanner(banner);
        return AjaxResult.success(banner.getBannerId());
    }

    /**
     * 查询主页轮播图
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('system:banner:add')")
   @GetMapping("/selectAll")
    public AjaxResult selectAll(){
        return AjaxResult.success(bannerService.selectAll());
    }


    /**
     * 根据主键查询轮播图对应得软文
     * @param bannerId
     * @return
     */
    @PostMapping("/selectAdver")
    public AjaxResult selectAdvertorialByBannerId(@RequestParam Long bannerId){
        return AjaxResult.success(bannerService.selectAdvertorialByBannerId(bannerId));
    }

    /**
     * 删除【根据主键删除轮播图】
     */
    @PreAuthorize("@ss.hasPermi('system:banner:deleteBannerById')")
    @Log(title = "【删除轮播图】", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult deleteBannerById(@RequestParam Long bannerId){
//        try {
//            bannerService.deleteBannerByBannerId(bannerId);
//            return  AjaxResult.success();
//        }catch (Exception e){
//            return AjaxResult.error();
//        }
        Banner banner = new Banner();
        banner.setBannerUrl(bannerService.selectBannerByBannerId(bannerId).getBannerUrl());
        File file = new File(banner.getBannerUrl());
        if (file.isFile() && file.exists()){
            file.delete();
            return AjaxResult.success(bannerService.deleteBannerByBannerId(bannerId));
        }else{
            return AjaxResult.error();
        }

    }






}
